package com.phospherus.customerinfo.service.impl;

import com.google.gson.Gson;
import com.phospherus.common.constant.Constant;
import com.phospherus.common.service.impl.BaseServiceImpl;
import com.phospherus.customerinfo.entity.BaseMessage;
import com.phospherus.customerinfo.entity.CreditReport;
import com.phospherus.customerinfo.repostory.CreditReportRepository;
import com.phospherus.customerinfo.service.ICreditReportService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by lipeng on 17/11/28
 */
@Service
public class CreditReportServiceImpl extends BaseServiceImpl<CreditReport,Long,CreditReportRepository>
implements ICreditReportService{
    @Autowired
    private RestTemplate restTemplate;
    @Override
    public CreditReport findbyKeyno(String keyno) throws Exception {
        return repository.findByKeyno(keyno);
    }

    /**
     * 通过keyno进行报告下单，若数据库中存在对应的keyno的下单信息，并且过期时间晚于当前日期
     * 则直接返回报告URL
     * 否则调用企查查接口进行报告下单，并存储相关信息，设置过期时间为60天后
     * @param keyno 公式在企查查的识别号
     * @return 信用报告URL信息
     */
    @Override
    public BaseMessage getCreditReport(String keyno) throws Exception{
        CreditReport report = null;
        BaseMessage basemessage = null;
        try {
            report = this.findbyKeyno(keyno);
        }catch (Exception ex){
            ex.printStackTrace();
        }
        if(report==null||report.getExpireDate().before(new Date())){
            try{
                System.out.println(Constant.BASE_URL+Constant.API_ORDER_CREDITREPORT+keyno+Constant.API_TOKEN_KEY_STRING);
                String json_message = restTemplate.postForObject(Constant.BASE_URL+Constant.API_ORDER_CREDITREPORT+keyno+Constant.API_TOKEN_KEY_STRING,new HashMap(),String.class);
                if(StringUtils.isNotEmpty(json_message)){
                    Gson json = new Gson();
                    basemessage = json.fromJson(json_message,BaseMessage.class);
                    //返回状态码为200表示下单成功
                    if(Constant.REST_API_STATUS.equals(basemessage.getStatus())) {
                        report = new CreditReport();
                        report.setKeyno(keyno);
                        report.setOrdercode(basemessage.getResult().get("OrderNo").toString());
                        //设置下载地址全路径
                        basemessage.getResult().put("OrderNo",Constant.REPORT_ENGINE_URL+report.getOrdercode());
                        basemessage.setStatus(Constant.REST_REPORTING_STATUS);
                        basemessage.setMessage("报告生成中，请五分钟尝试再次下载");
                        Calendar cl = Calendar.getInstance();
                        //增加过期时间，和技术支持沟通后，得知报告存储有效期为60天
                        cl.add(Calendar.DATE,Constant.EXPIRE_DATE);
                        report.setExpireDate(cl.getTime());
                        if(report.getId()!=null)
                          this.repository.delete(report.getId());
                        this.repository.save(report);
                    }
                }
            }catch (Exception ex){
                ex.printStackTrace();
            }
        }else {
            basemessage = new BaseMessage();
            basemessage.setStatus(Constant.REST_API_STATUS);
            basemessage.setMessage("下单成功");
            Map map = new HashMap();
            map.put("OrderNo",Constant.REPORT_ENGINE_URL+report.getOrdercode());
            basemessage.setResult(map);
        }
        return basemessage;
    }

}
